python教程分享Pytorch实现常用乘法算子TensorRT的示例代码

您所在的位置:网站首页 pytorch 源码阅读 python教程分享Pytorch实现常用乘法算子TensorRT的示例代码

python教程分享Pytorch实现常用乘法算子TensorRT的示例代码

#python教程分享Pytorch实现常用乘法算子TensorRT的示例代码| 来源: 网络整理| 查看: 265

python教程分享Pytorch实现常用乘法算子TensorRT的示例代码介绍一下 pytorch 中常用乘法的 tensorrt 实现。

pytorch 用于训练,tensorrt 用于推理是很多 ai 应用开发的标配。大家往往更加熟悉 pytorch 的算子,而不太熟悉 tensorrt 的算子,这里拿比较常用的乘法运算在两种框架下的实现做一个对比,可能会有更加直观一些的认识。

1.乘法运算总览

先把 pytorch 中的一些常用的乘法运算进行一个总览:

torch.mm:用于两个矩阵 (不包括向量) 的乘法,如维度 (m, n) 的矩阵乘以维度 (n, p) 的矩阵; torch.bmm:用于带 batch 的三维向量的乘法,如维度 (b, m, n) 的矩阵乘以维度 (b, n, p) 的矩阵; torch.mul:用于同维度矩阵的逐像素点相乘,也即点乘,如维度 (m, n) 的矩阵点乘维度 (m, n) 的矩阵。该方法支持广播,也即支持矩阵和元素点乘; torch.mv:用于矩阵和向量的乘法,矩阵在前,向量在后,如维度 (m, n) 的矩阵乘以维度为 (n) 的向量,输出维度为 (m); torch.matmul:用于两个张量相乘,或矩阵与向量乘法,作用包含 torch.mm、torch.bmm、torch.mv; @:作用相当于 torch.matmul; *:作用相当于 torch.mul;

如上进行了一些具体罗列,可以归纳出,常用的乘法无非两种:矩阵乘 和 点乘,所以下面分这两类进行介绍。

2.乘法算子实现

2.1矩阵乘算子实现

先来看看矩阵乘法的 pytorch 的实现 (以下实现在终端):

>>> import torch >>> # torch.mm >>> a = torch.randn(66, 99) >>> b = torch.randn(99, 88) >>> c = torch.mm(a, b) >>> c.shape torch.size([66, 88]) >>> >>> # torch.bmm >>> a = torch.randn(3, 66, 99) >>> b = torch.randn(3, 99, 77) >>> c = torch.bmm(a, b) >>> c.shape torch.size([3, 66, 77]) >>> >>> # torch.mv >>> a = torch.randn(66, 99) >>> b = torch.randn(99) >>> c = torch.mv(a, b) >>> c.shape torch.size([66]) >>> >>> # torch.matmul >>> a = torch.randn(32, 3, 66, 99) >>> b = torch.randn(32, 3, 99, 55) >>> c = torch.matmul(a, b) >>> c.shape torch.size([32, 3, 66, 55]) >>> >>> # @ >>> d = a @ b >>> d.shape torch.size([32, 3, 66, 55])

来看 tensorrt 的实现,以上乘法都可使用 addmatrixmultiply 方法覆盖,对应 torch.matmul,先来看该方法的定义:

//! //! brief add a matrixmultiply layer to the network. //! //! param input0 the first input tensor (commonly a). //! param op0 the operation to apply to input0. //! param input1 the second input tensor (commonly b). //! param op1 the operation to apply to input1. //! //! see imatrixmultiplylayer //! //! warning int32 tensors are not valid input tensors. //! //! return the new matrix multiply layer, or nullptr if it could not be created. //! imatrixmultiplylayer* addmatrixmultiply( itensor& input0, matrixoperation op0, itensor& input1, matrixoperation op1) noexcept { return mimpl->addmatrixmultiply(input0, op0, input1, op1); }

可以看到这个方法有四个传参,对应两个张量和其 operation。来看这个算子在 tensorrt 中怎么添加:

// 构造张量 tensor0 nvinfer1::iconstantlayer *constant_layer0 = m_network->addconstant(tensorshape0, value0); // 构造张量 tensor1 nvinfer1::iconstantlayer *constant_layer1 = m_network->addconstant(tensorshape1, value1); // 添加矩阵乘法 nvinfer1::imatrixmultiplylayer *matmul_layer = m_network->addmatrixmultiply(constant_layer0->getoutput(0), matrix0type, constant_layer1->getoutput(0), matrix2type); // 获取输出 matmuloutput = matmul_layer->getoputput(0);

2.2点乘算子实现

再来看看点乘的 pytorch 的实现 (以下实现在终端):

>>> import torch >>> # torch.mul >>> a = torch.randn(66, 99) >>> b = torch.randn(66, 99) >>> c = torch.mul(a, b) >>> c.shape torch.size([66, 99]) >>> d = 0.125 >>> e = torch.mul(a, d) >>> e.shape torch.size([66, 99]) >>> # * >>> f = a * b >>> f.shape torch.size([66, 99])

来看 tensorrt 的实现,以上乘法都可使用 addscale 方法覆盖,这在图像预处理中十分常用,先来看该方法的定义:

//! //! brief add a scale layer to the network. //! //! param input the input tensor to the layer. //! this tensor is required to have a minimum of 3 dimensions in implicit batch mode //! and a minimum of 4 dimensions in explicit batch mode. //! param mode the scaling mode. //! param shift the shift value. //! param scale the scale value. //! param power the power value. //! //! if the weights are available, then the size of weights are dependent on the scalemode. //! for ::kuniform, the number of weights equals 1. //! for ::kchannel, the number of weights equals the channel dimension. //! for ::kelementwise, the number of weights equals the product of the last three dimensions of the input. //! //! see addscalend //! see iscalelayer //! warning int32 tensors are not valid input tensors. //! //! return the new scale layer, or nullptr if it could not be created. //! iscalelayer* addscale(itensor& input, scalemode mode, weights shift, weights scale, weights power) noexcept { return mimpl->addscale(input, mode, shift, scale, power); }

 可以看到有三个模式:

kuniform:weights 为一个值,对应张量乘一个元素; kchannel:weights 维度和输入张量通道的 c 维度对应,可以做一些以通道为基准的预处理; kelementwise:weights 维度和输入张量的 c、h、w 对应,不考虑 batch,所以是输入的后三维;

再来看这个算子在 tensorrt 中怎么添加:

// 构造张量 input nvinfer1::iconstantlayer *constant_layer = m_network->addconstant(tensorshape, value); // scalemode选择,kuniform、kchannel、kelementwise scalemode = kuniform; // 构建 weights 类型的 shift、scale、power,其中 volume 为元素数量 nvinfer1::weights scaleshift{nvinfer1::datatype::kfloat, nullptr, volume }; nvinfer1::weights scalescale{nvinfer1::datatype::kfloat, nullptr, volume }; nvinfer1::weights scalepower{nvinfer1::datatype::kfloat, nullptr, volume }; // !! 注意这里还需要对 shift、scale、power 的 values 进行赋值,若只是乘法只需要对 scale 进行赋值就行 // 添加张量乘法 nvinfer1::iscalelayer *scale_layer = m_network->addscale(constant_layer->getoutput(0), scalemode, scaleshift, scalescale, scalepower); // 获取输出 scaleoutput = scale_layer->getoputput(0);

有一点你可能会比较疑惑,既然是点乘,那么输入只需要两个张量就可以了,为啥这里有 input、shift、scale、power 四个张量这么多呢。解释一下,input 不用说,就是输入张量,而 shift 表示加法参数、scale 表示乘法参数、power 表示指数参数,说到这里,你应该能发现,这个函数除了我们上面讲的点乘外还有其他更加丰富的运算功能。

到此这篇关于pytorch实现常用乘法算子tensorrt的示例代码的文章就介绍到这了,更多相关pytorch乘法算子tensorrt内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!

需要了解更多python教程分享Pytorch实现常用乘法算子TensorRT的示例代码,都可以关注python教程分享栏目—编程笔记



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3